package com.bestcem.xm.member.entity.pojo;

import com.alibaba.fastjson.JSONObject;
import com.bestcem.xm.member.entity.mysql.MemberEntity;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Date;


@Getter
@Setter
@ToString
public class MemberDO {
    private String id;

    /**
     * 组织id
     */
    private String orgId;

    /**
     * 联系人类型 0-个人联系人 1-机构联系人 2-内部联系人 3-隐藏的 4-项目
     */
    private Integer memberType;

    /**
     * 联系人状态 0-活动的 1-禁用的 2-注销的 3-隐藏的 4-删除
     */
    private Integer status;

    /**
     * 系统编号
     */
    private String sysId;

    /**
     * 来源
     */
    private String source;

    /**
     * 微信头像url
     */
    private String avatar;

    /**
     * 联系人姓名
     */
    private String name;

    /**
     * 微信昵称
     */
    private String nickname;

    /**
     * 电子邮箱 (邮箱与手机号必有其一)
     */
    private String email;

    /**
     * 手机号码 (邮箱与手机号必有其一)
     */
    private String phone;

    /**
     * 性别
     */
    private String gender;

    private String region;

    /**
     * 所属门店id
     */
    private String storeId;

    /**
     * 备注
     */
    private String content;

    /**
     * 微信openId
     */
    private String openId;

    /**
     * 微信appId
     */
    private String appId;

    /**
     * 微信unionId
     */
    private String unionId;

    /**
     * 支付宝用户标识
     */
    private String aliPayUserId;

    /**
     * 联系人编号
     */
    private String memberNo;

    /**
     * 地址信息
     */
    private String address;

    /**
     * 员工编号
     */
    private String employeeId;

    /**
     * 雇佣状态 0-无状态 1-在职 2-离职
     */
    private Integer employeeStatus;

    /**
     * 生日
     */
    private Date birthday;

    /**
     * 所属部门
     */
    private String department;

    /**
     * 职位
     */
    private String position;

    /**
     * 机构编号
     */
    private String instituteId;

    /**
     * 机构名称
     */
    private String instituteName;

    /**
     * 用户标识
     */
    private String userId;

    /**
     * 年龄
     */
    private Integer age;

    /**
     * 联系人标签
     */
    private String labels;

    /**
     * 联系人扩展字段
     */
    private String fieldValList;

    private String groupSyncToken;

    /**
     * 联系人分组
     */
    private String groups;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 更新时间
     */
    private Date updateTime;

    private String outNo;

    public static MemberDO toDo(MemberEntity entity) {
        if (entity == null) {
            return null;
        }
        MemberDO memberDo = new MemberDO();
        memberDo.setAddress(entity.getAddress());
        memberDo.setAge(entity.getAge());
        memberDo.setAliPayUserId(entity.getAliPayUserId());
        memberDo.setAppId(entity.getAppId());
        memberDo.setUnionId(entity.getUnionId());
        memberDo.setBirthday(entity.getBirthday());
        memberDo.setContent(entity.getContent());
        memberDo.setCreateTime(entity.getCreateTime());
        memberDo.setDepartment(entity.getDepartment());
        memberDo.setEmail(entity.getEmail());
        memberDo.setEmployeeId(entity.getEmployeeId());
        memberDo.setEmployeeStatus(entity.getEmployeeStatus());
        memberDo.setFieldValList(entity.getFieldValList());
        memberDo.setGender(entity.getGender());
        memberDo.setId(entity.getId());
        memberDo.setInstituteId(entity.getInstituteId());
        memberDo.setInstituteName(entity.getInstituteName());
        memberDo.setLabels(entity.getLabels());
        memberDo.setMemberNo(entity.getMemberNo());
        memberDo.setMemberType(entity.getMemberType());
        memberDo.setAvatar(entity.getAvatar());
        memberDo.setName(entity.getName());
        memberDo.setNickname(entity.getNickname());
        memberDo.setOpenId(entity.getOpenId());
        memberDo.setOrgId(entity.getOrgId());
        memberDo.setPhone(entity.getPhone());
        memberDo.setPosition(entity.getPosition());
        memberDo.setSource(entity.getSource());
        memberDo.setStatus(entity.getStatus());
        memberDo.setStoreId(entity.getStoreId());
        memberDo.setSysId(entity.getSysId());
        memberDo.setUpdateTime(entity.getUpdateTime());
        memberDo.setUserId(entity.getUserId());
        memberDo.setRegion(entity.getRegion());
        memberDo.setGroups(entity.getGroups());
        memberDo.setGroupSyncToken(entity.getGroupSyncToken());
        memberDo.setOutNo(entity.getOutNo());
        return memberDo;
    }

    public static MemberEntity toEntity(MemberDO memberDo) {
        if (memberDo == null) {
            return new MemberEntity();
        }
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setAddress(memberDo.getAddress());
        memberEntity.setAge(memberDo.getAge());
        memberEntity.setAliPayUserId(memberDo.getAliPayUserId());
        memberEntity.setAppId(memberDo.getAppId());
        memberEntity.setUnionId(memberDo.getUnionId());
        memberEntity.setAvatar(memberDo.getAvatar());
        memberEntity.setBirthday(memberDo.getBirthday());
        memberEntity.setContent(memberDo.getContent());
        memberEntity.setCreateTime(memberDo.getCreateTime());
        memberEntity.setDepartment(memberDo.getDepartment());
        memberEntity.setEmail(memberDo.getEmail());
        memberEntity.setEmployeeId(memberDo.getEmployeeId());
        memberEntity.setEmployeeStatus(memberDo.getEmployeeStatus());
        memberEntity.setFieldValList(memberDo.getFieldValList());
        memberEntity.setGender(memberDo.getGender());
        memberEntity.setId(memberDo.getId());
        memberEntity.setInstituteId(memberDo.getInstituteId());
        memberEntity.setInstituteName(memberDo.getInstituteName());
        memberEntity.setLabels(memberDo.getLabels());
        memberEntity.setMemberNo(memberDo.getMemberNo());
        memberEntity.setMemberType(memberDo.getMemberType());
        memberEntity.setName(memberDo.getName());
        memberEntity.setNickname(memberDo.getNickname());
        memberEntity.setOpenId(memberDo.getOpenId());
        memberEntity.setOrgId(memberDo.getOrgId());
        memberEntity.setPhone(memberDo.getPhone());
        memberEntity.setPosition(memberDo.getPosition());
        memberEntity.setSource(memberDo.getSource());
        memberEntity.setStatus(memberDo.getStatus());
        memberEntity.setStoreId(memberDo.getStoreId());
        memberEntity.setSysId(memberDo.getSysId());
        memberEntity.setUpdateTime(memberDo.getUpdateTime());
        memberEntity.setUserId(memberDo.getUserId());
        memberEntity.setRegion(memberDo.getRegion());
        // 增加默认值
        memberEntity.setGroups(StringUtils.isNotBlank(memberDo.getGroups()) ? memberDo.getGroups() : JSONObject.toJSONString(new ArrayList<>()));
        memberEntity.setGroupSyncToken(memberDo.getGroupSyncToken());
        // 增加默认值
        memberEntity.setOutNo(StringUtils.isNotBlank(memberDo.getOutNo()) ? memberDo.getOutNo() : "");
        return memberEntity;
    }

}
